<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="cake.dao.OrderDetailDao"%>
<%@page import="cake.db.MySqlDb"%>
<%@page import="java.sql.ResultSet"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>MyOrders page</title>
		<link rel="stylesheet" href="styles/style.css" type="text/css" />
		<script src="scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
		<script src="scripts/jquery.select.js" type="text/javascript"></script>
		<script src="scripts/tools.js" type="text/javascript"></script>
		<script src="scripts/script.js" type="text/javascript"></script>
	</head>
	<body>
		<div id="container">
			<div id="header">
				<div id="header_left">
					<img src="images/logo.png" alt="cake"  class="logo"/>
				</div>
				<div style="padding-top: 20px;">
					<input type="text" id="keywords" name="keywords" />
					<input type="image" id="search" src="images/search.jpg" value="search"  onclick="search();"/>
					<span class=" btn" style="margin-left: 50px;"> <%
 	String username = null;
 	long userid = 0;
 	username = (String) session.getAttribute("username");
 	String tempS=(String)session.getAttribute("userid");
 	userid = Long.parseLong(tempS);
 	if (username == null || username.equals("")) {
 		response.sendRedirect("login.jsp");
 	} else {
 %> Welcome: <%=username%> &nbsp;&nbsp;&nbsp;&nbsp; <span
						class="pointer" onclick="toURL('myOrder.jsp')">MyOrders |</span>
						<span class="pointer" onclick="quit();">Quit</span> <%
 	}
 %> </span>
 <span style="margin-left:20px;cursor:pointer;" onclick="toURL('msgBoard.jsp');">MsgBoard</span>
				</div>
				<div id="navigation">
					<ul>
						<li><a href="index.jsp">Home</a> |</li>
						<li><a href="cakes.jsp">Reservation</a> |</li>
						<li><a href="advanced.jsp">Advanced Reservation</a> |</li>
						<li><a href="cakes.jsp?category=5">New Products</a> |</li>
						<%
						if(username==null || username.equals(""))
						{
						%>
						<li><a href="login.jsp">MyOrders</a> |</li>
						<li><a href="login.jsp">Shopping Cart</a> |</li>
						<%
						}
						else
						{
						%>
						<li><a href="myOrder.jsp">MyOrders</a> |</li>
						<li><a href="shoppingCart.jsp">Shopping Cart</a> |</li>
						<%
						}
						%>
						<li><a href="about.jsp">About us</a></li>
					</ul>
				</div>
			</div>
			<div id="content">
				<div style="margin-left: 35px;">
					<div>
						Location：
						<a href="index.jsp">Home</a> &gt; My Orders
					</div>
					<div style="padding: 40px 10px 40px 10px;">
						<div>
							<span id="switch1" class="switch_btn1" onclick="showOrderList();">Order List</span><span id="switch2" class="switch_btn2" onclick="showDiyList();">DIY List</span>
							<span style="margin-left:300px;">all successful order will be send in 2 hours</span>
							<div id="orderList" >
							<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse;" bordercolor="#CCCCCC">
								<tr style="background-color:#FCF2E4;">
									<td width="30">
										Index
									</td>
									<td width="150">
										Code
									</td>
									<td width="60">
										Amount
									</td>
									<td width="200">
										Cake
									</td>
									<td width="130">
										Address
									</td>
									<td width="90">
										Phone
									</td>
									<td width="100">
										Remark
									</td>
									<td width="60">
										Status
									</td>
									<td width="110">
										Operation
									</td>
								</tr>
								<%
									String currPage = request.getParameter("page");
									int pageSize = 20;
									int pageNum = 1;
									if (currPage != null && !currPage.equals("")) {
										pageNum = Integer.parseInt(currPage);
									}
									MySqlDb db = new MySqlDb();

									String sql = "select count(*) from myorder left outer join orderdetail on myorder.id=orderdetail.orderid"
											+ " left outer join cake on orderdetail.cakeid=cake.id where myorder.userid="
											+ userid;

									ResultSet rs = db.query(sql);

									int total = 0;
									while (rs.next()) {
										total = rs.getInt(1);
										break;
									}
									int totalPage = 0;
									if (total % pageSize == 0) {
										totalPage = total / pageSize;
									} else {
										totalPage = total / pageSize + 1;
									}

									sql = "select myorder.*,cake.name,cake.price,cake.picture,orderdetail.quantity from myorder "
											+ " left outer join orderdetail on myorder.id=orderdetail.orderid left outer join cake "
											+ " on orderdetail.cakeid=cake.id where myorder.userid="
											+ userid
											+ " order by status asc limit "
											+ (pageNum - 1)
											* pageSize
											+ ","
											+ pageSize;

									rs = db.query(sql);
									int index = 1;
									while (rs.next()) {
								%>
								<tr id="order<%=rs.getLong("id") %>">
									<td><%=index%></td>
									<td><%=rs.getString("code")%></td>
									<td><%=rs.getFloat("amount")%></td>
									<td>
										<img src="<%=rs.getString("picture")%>"
											alt="<%=rs.getString("name")%>"
											style="float: left; width: 50px; height: 50px;" />
										<b>name:</b><%=rs.getString("name")%><br/>
										<b>price:</b><%=rs.getFloat("price")%>
									</td>
									<td><%=rs.getString("address")%></td>
									<td><%=rs.getString("phone")%></td>
									<td><%=rs.getString("remark")%></td>
									<td>
										<%
											int status = rs.getInt("status");
												String temp = "";
												String operation="";
												switch (status) {
													case 0 :
														temp = "ordered";
														operation = "<a href='updateOrder.jsp?orderid="+rs.getLong("id")
																+"'>update</a> | <span class='pointer' onclick='cancelOrder("+rs.getLong("id")+");'>cancel</span>";
														break;
													case 1 :
														temp = "sended";
														operation = "<span class='pointer' onclick='confirmOrder("+rs.getLong("id")+");'>confirm</span>";
														break;
													case 2 :
														temp = "successed";
														operation = "<span class='pointer' onclick='comment("+rs.getLong("id")+");'>comment</span>";
														break;
													case 3 :
														temp = "failed";
														break;
												}
										%>
										<%=temp%>
									</td>
									<td>
										<%=operation %>
									</td>
								</tr>
								<%
									index++;
									}
								%>
							</table>
							<div>
								<%
									if (totalPage > 1) {
										for (int i = 1; i <= totalPage; i++) {
											if (i == 1) {
								%>
								<span><a href="myOrder.jsp?page=<%=i%>">[FirstPage]</a> </span>
								<%
									} else if (i == totalPage) {
								%>
								<span><a href="myOrder.jsp?page=<%=i%>">[LastPage]</a> </span>
								<%
									} else {
								%>
								<span><a href="myOrder.jsp?page=<%=i%>"><%=i%></a> </span>
								<%
									}
										}
									}
								%>
							</div>
							</div>
							<div id="diyList" style="display:none;">
								<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse;" bordercolor="#CCCCCC">
								<tr style="background-color:#FCF2E4;">
									<td width="30">
										Index
									</td>
									<td width="150">
										Code
									</td>
									<td width="60">
										Amount
									</td>
									<td width="250">
										Description
									</td>
									<td width="130">
										Address
									</td>
									<td width="90">
										Phone
									</td>
									<td width="60">
										Remark
									</td>
									<td width="60">
										Status
									</td>
									<td width="100">
										Operation
									</td>
								</tr>
								<%

									sql = "select * from diy where userid="+userid+" order by status asc";

									rs = db.query(sql);
									index = 1;
									while (rs.next()) {
								%>
								<tr>
									<td><%=index%></td>
									<td><%=rs.getString("code")%></td>
									<td><%=rs.getFloat("amount")%></td>
									<td>
										<a href="<%=rs.getString("picture")%>" target="blank"><img src="<%=rs.getString("picture")%>"
											alt="diy"
											style="float: left; width: 100px; height: 100px;" /></a>
											<%=rs.getString("description") %>
									</td>
									<td><%=rs.getString("address")%></td>
									<td><%=rs.getString("phone")%></td>
									<td><%=rs.getString("remark")%></td>
									<td>
										<%
											int status = rs.getInt("status");
												String temp = "";
												String operation="";
												switch (status) {
													case 0 :
														temp = "ordered";
														operation = "<span class='pointer' onclick='cancelDiyOrder("+rs.getLong("id")+");'>cancel</span>&nbsp;<span class='pointer' onclick='toURL(\"updatePicture.jsp?id="+rs.getLong("id")+"\");'>upate</span>";
														break;
													case 1 :
														temp = "sended";
														operation = "<span class='pointer' onclick='confirmDiyOrder("+rs.getLong("id")+");'>confirm</span>";
														break;
													case 2 :
														temp = "successed";
														break;
													case 3 :
														temp = "failed";
														break;
												}
										%>
										<%=temp%>
									</td>
									<td>
										<%=operation %>
									</td>
								</tr>
								<%
									index++;
									}
								%>
							</table>
							</div>
						</div>
					</div>
				</div>
				<div id="categorys">
					<%@include file="footer.jsp" %>
				</div>
			</div>
		</div>
	</body>
</html>
